package com.peanut.baby.mvp.livedetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.http.ApiException;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.liveroom.LiveRoomActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.main.expert.live.LiveRecyclerAdapter;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LiveListActivity extends BaseActivity implements TitleLayout.OnTitleClickListener,
        PullRecyclerView.OnRecyclerRefreshListener,
        LiveListContract.View,LiveRecyclerAdapter.OnItemViewClickListener{
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.bbs_list)
    PullRecyclerView recycle;
    LiveListPresenter presenter;

    private int pageNo  = 1;
    private int pageCount = 5;
    private LiveRecyclerAdapter adapter;
    private List<LiveRoom>  rooms;


    public static void start(Context ctx){
        ctx.startActivity(new Intent(ctx,LiveListActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
        ButterKnife.bind(this);

        title.setOnTitleClickedListener(this);
        title.setTitle("直播间");
        recycle.setOnRecyclerRefreshListener(this);
        presenter = new LiveListPresenter(this,this);
        rooms = new ArrayList<>();
        adapter = new LiveRecyclerAdapter(this,rooms);
        adapter.setOnClickListener(this);
        recycle.setLayoutManager(new XLinearLayoutManager(this));
        recycle.setAdapter(adapter);

        recycle.postRefreshing();

    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getLiveList(pageNo,pageCount);
    }

    @Override
    public void onLoadMore() {
        pageNo += 1;
        presenter.getLiveList(pageNo,pageCount);
    }

    @Override
    public void onLiveListGet(List<LiveRoom> _rooms) {
        if(_rooms != null && !_rooms.isEmpty()){
            if(pageNo == 1){
                rooms.clear();
            }
            rooms.addAll(_rooms);
            adapter.notifyDataSetChanged();
            recycle.enableLoadMore(_rooms.size() >= pageCount); // 当剩余还有大于0页的数据时，开启上拉加载更多
        }
        recycle.stopRefresh();
        recycle.stopLoadMore();
    }

    @Override
    public void onGetLiveListFailed(int pageNo, String error) {
        showToast(error);
        recycle.stopRefresh();
        recycle.stopLoadMore();
    }

    @Override
    public void onJoinSuccess(LiveRoom room) {
        dismissProgressDialog();
        room.isEnroled = 1;
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onJoinFailed(String code, String reason) {
        dismissProgressDialog();
        if(!ApiException.isApiException(code)){
            showToast(reason);
        }
    }

    @Override
    public void onViewItemClicked(View v, LiveRoom room) {
        switch (v.getId()){
            case R.id.live_container:
                LiveDetailActivity.start(this,room.roomId);
                break;
            case R.id.live_join:
                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(this);
                    return;
                }
                if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
                    BindMobileActivity.startForResult(this);
                    return;
                }

//                //3.1 added 如果不考虑报名状态 注释删掉
//                if(room.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
//                    LiveRoomActivity.start(LiveListActivity.this,room);
//                    return;
//                }

                if(InitManager.getInstance().getUserId().equals(room.expertId)
                        || InitManager.getInstance().getUserId().equals(room.serverId)){
                    LiveDetailActivity.start(this,room.roomId);
                }else{
                    if(room.isEnroled == 0){
                        showProgressDialog("正在报名...",false);
                        presenter.joinRoom(room);
                    }else{
                        LiveRoomActivity.start(this,room);
                    }
                }

                break;
        }
    }
}
